package com.carlinfo.stu_ques.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.carlinfo.stu_ques.common.util.DateUtile;
import com.carlinfo.stu_ques.demo.dao.impl.DynastyDaoImpl;
import com.carlinfo.stu_ques.demo.dao.impl.KingDaoimpl;
import com.carlinfo.stu_ques.demo.pojo.Dynasty;
import com.carlinfo.stu_ques.demo.pojo.King;
import com.carlinfo.stu_ques.demo.service.IDemoService;
import com.carlinfo.stu_ques.demo.service.impl.DemoServiceImpl;
import com.carlinfo.stu_ques.question.dao.IAnswerDao;
import com.carlinfo.stu_ques.question.dao.IQuestionDao;
import com.carlinfo.stu_ques.question.dao.impl.AnswerDaoImpl;
import com.carlinfo.stu_ques.question.dao.impl.QuestionDaoImpl;
import com.carlinfo.stu_ques.question.pojo.Answer;
import com.carlinfo.stu_ques.question.pojo.Question;
import com.carlinfo.stu_ques.question.service.IQuestionService;
import com.carlinfo.stu_ques.question.service.impl.QuestionServiceImpl;
import com.carlinfo.stu_ques.users.dao.IGrade;
import com.carlinfo.stu_ques.users.dao.IStudentsDao;
import com.carlinfo.stu_ques.users.dao.impl.GradeDaoimpl;
import com.carlinfo.stu_ques.users.dao.impl.StudentDaosimpl;
import com.carlinfo.stu_ques.users.pojo.Grade;
import com.carlinfo.stu_ques.users.pojo.Students;
import com.carlinfo.stu_ques.users.service.Istudentsservice;
import com.carlinfo.stu_ques.users.service.impl.StudentsService;
import java.sql.Connection;

public class ClientMain
{
	private IDemoService demoService ;

	private IQuestionService questionService ;

	private Istudentsservice studentservice ;

	DateUtile dateutile = new DateUtile();

	public static void main(String[] args)
	{
		ClientMain clientmain = new ClientMain();
		IDemoService demoService = new DemoServiceImpl();
		clientmain.setDemoService(demoService);
		if(demoService instanceof DemoServiceImpl ) 
		{
			DemoServiceImpl demoServiceImpl = (DemoServiceImpl) demoService;
			DynastyDaoImpl dynasty = new DynastyDaoImpl();
			KingDaoimpl king = new KingDaoimpl();
			demoServiceImpl.setKing(king);
			demoServiceImpl.setDynasty(dynasty);
		}
		IQuestionService questionService = new QuestionServiceImpl();
		clientmain.setQuestionService(questionService);
		Istudentsservice studentservice = new StudentsService();
		if(questionService instanceof QuestionServiceImpl)
		{
			QuestionServiceImpl questionServiceImpl = (QuestionServiceImpl) questionService;
			IAnswerDao answer = new AnswerDaoImpl();
			IQuestionDao question = new QuestionDaoImpl();
			questionServiceImpl.setAnswer(answer);
			questionServiceImpl.setQuestion(question);
			questionServiceImpl.setStudntsservice(studentservice);
		}
		
		clientmain.setStudentservice(studentservice);
		if(studentservice instanceof StudentsService)
		{
			StudentsService studentsService = (StudentsService) studentservice;
			IStudentsDao student = new StudentDaosimpl();
			IGrade gradedaoimpl = new GradeDaoimpl();
			studentsService.setStudent(student);
			studentsService.setGradedaoimpl(gradedaoimpl);
			
		}
		clientmain.lanuch();
	}

	private void lanuch()
	{
		BufferedReader br = null;
		try
		{
			System.out.println("==请输入：==");
			/* 将管子接到键盘上 */
			InputStream in = System.in;
			/*
			 * 参数要Reader,但是我只有字符流(InputStream) 如何将inputStream变成Reader呢
			 */
			InputStreamReader isr = new InputStreamReader(in, "UTF-8");
			/*
			 * 键盘上输入的都是字符流 所以用最牛逼的方法，一次读一行 readLine
			 */
			br = new BufferedReader(isr);
			String line = null;
			while (true)
			{
				System.out.println("==主菜单:==按n停止(不区分大小写);按下h(help)可以查看帮助;");
				/*
				 * 接受键盘输入的值 字符串
				 */
				line = br.readLine();
				/*
				 * 去掉前后空格
				 */
				line = line.trim();
				if ("n".equalsIgnoreCase(line))
				{
					System.out.println("谢谢您的使用，欢迎下次光临！");
					break;
				} else if ("h".equalsIgnoreCase(line))
				{
					help();
				} else if ("1".equalsIgnoreCase(line))
				{
					/* 查询所有朝代 */
					this.selectListDynastyService();
				} else if ("2".equalsIgnoreCase(line))
				{
					/* 保存一个 朝代 */
					this.saveOneService(br);
				} else if ("3".equalsIgnoreCase(line))
				{
					/* 修改一个朝代,修改朝代;(先输入朝代id,填写要修改的内容) */
					this.updateOneDynastyService(br);
				} else if ("4".equalsIgnoreCase(line))
				{
					/* 删除一个朝代 */
					this.deleteOneDynastyService(br);
				} else if ("5".equalsIgnoreCase(line))
				{
					/* 查看一个朝代 */
					this.selectOneDynastyService(br);
				} else if ("6".equalsIgnoreCase(line))
				{
					/* 查询所有皇上 */
					this.selectListKingService();

				} else if ("7".equalsIgnoreCase(line))
				{
					/* 添加皇上 */
					this.savaOneKingService(br);
				} else if ("8".equalsIgnoreCase(line))
				{
					/* 修改皇上;(先输入皇上id,填写要修改的内容) */
					this.updateOneKingService(br);
				} else if ("9".equalsIgnoreCase(line))
				{
					/* 删除皇上 */
					this.deleteOneKingService(br);
				} else if ("10".equalsIgnoreCase(line))
				{
					/* 查询一个皇上 */
					this.selectOneKingService(br);
				} else if ("11".equalsIgnoreCase(line))
				{
					/* 增加一个学生 */
					this.savaStudentsOneDao(br);
				} else if ("12".equalsIgnoreCase(line))
				{
					/* 查询所有学生 */
					this.selectStudentsListDao();
				} else if ("13".equalsIgnoreCase(line))
				{
					/* 查询一个学生 */
					this.selectStudentsOneDao(br);
				} else if ("14".equalsIgnoreCase(line))
				{
					/* 修改一个学生 */
					this.updateStudentsOneDao(br);
				} else if ("15".equalsIgnoreCase(line))
				{
					/* 删除一个学生 */
					this.delectStudentsOneDao(br);
				} else if ("16".equalsIgnoreCase(line))
				{
					/* 增加一个学生等级 */
					this.savaGradeOneDao(br);
				} else if ("17".equalsIgnoreCase(line))
				{
					/* 查询所有学生等级 */
					this.selectGradeListDao();
				} else if ("18".equalsIgnoreCase(line))
				{
					/* 查询一个学生等级 */
					this.selectGradeOneDao(br);
				} else if ("19".equalsIgnoreCase(line))
				{
					/* 修改一个学生等级 */
					this.updateGradeOneDao(br);
				} else if ("20".equalsIgnoreCase(line))
				{
					/* 删除一个学生等级 */
					this.delectGradeOneDao(br);
				} else if ("21".equalsIgnoreCase(line))
				{
					this.selectListAnswer();
				} else if ("22".equalsIgnoreCase(line))
				{
					this.saveOneAnswer(br);
				} else if ("25".equalsIgnoreCase(line))
				{
					this.selectOneAnswer(br);
				} else if ("23".equalsIgnoreCase(line))
				{
					this.updateOneAnswer(br);
				} else if ("24".equalsIgnoreCase(line))
				{
					this.deleteOneAnswer(br);
				} else if ("26".equalsIgnoreCase(line))
				{
					this.selectListQuestion();
				} else if ("27".equalsIgnoreCase(line))
				{
					this.saveOneQuestion(br);
				} else if ("30".equalsIgnoreCase(line))
				{
					this.selectOneQuestion(br);
				} else if ("28".equalsIgnoreCase(line))
				{
					this.updateOneQuestion(br);
				} else if ("29".equalsIgnoreCase(line))
				{
					this.deleteOneQuestion(br);
				} else if ("50".equalsIgnoreCase(line))
				{
					randomAsk(br);
				}else if ("51".equalsIgnoreCase(line))
				{
					mysqlIo(br);
				}
			}

		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (br != null)
				{
					br.close();
					br = null;
				}
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	private void mysqlIo(BufferedReader br)
	{
		String url = "jdbc:mysql:///mydata?useSSL=false";
		String user = "root";
		String password = "123456";
		String tableName  = "";
		String driver = "com.mysql.jdbc.Driver";
		Connection con = null;
		PreparedStatement pre = null;
		ResultSet rs = null;
		try
		{	
			String line = br.readLine();
			line = line.trim();
			/* 加载驱动 */
			Class.forName(driver);
			/* 获取连接 */
			con = DriverManager.getConnection(url, user, password);
			if("1".equalsIgnoreCase(line))
			{
				tableName = "student";
			}
			String sql = "select * from "+tableName+";";
			/* 获取sql执行集合 */
			pre = con.prepareStatement(sql);
			System.out.println(line);
			/* 执行SQL语句,用rs接结果 */
			rs = pre.executeQuery();
			/* 循环结果集 */
			while(rs.next()) 
			{
				int id = rs.getInt(1);
				String name = rs.getString("name");
				 
				System.out.println(id + "\t" + name  ) ;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}finally 
		{
			/* 关闭连接 */
			try
			{
				if(rs != null)
				{
					rs.close();
					rs = null;
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			try
			{
				if(pre != null) 
				{
					pre.close();
					pre = null;
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			try
			{
				if(con != null) 
				{
					con.close();
					con = null;
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param br
	 */
	private void randomAsk(BufferedReader br)
	{
		try
		{
			System.out.println("请输入问题的ID");
			Question question = this.selectOneQuestion(br);
			while(true) 
			{
				if(question != null) 
				{
					int maxNum = this.studentservice.selectStudentMaxIdService();
					Random random = new Random();
					Students students = null;
					int count = 0;
					while(true) 
					{
						/* 随机抽取一个学生 */
						int tempId = random.nextInt(maxNum);
						/* 根据id查询学生 */
						Map<String, Object> condMap = new HashMap<String, Object>();
						condMap.put("id", tempId);
						students = this.studentservice.selectStudentsOneDao(condMap);
						/*if(students != null)*/
						if(this.studentservice.askIsStudentService(students))
						{
							/* 接着抽 */
							break; 
						}
						count ++ ; 
					}
					if(students != null) 
					{
						/* 打印一句话,此学生回答问题 */
						System.out.println(maxNum + "==来来来,恭喜你,回答问题==抽的次数:" + count);
						System.out.println(students);
						/* 
						 * 等待学生回答问题;
						 * 答案保存:
						 *  */
						System.out.println("==亲请输入答案的相关信息：（答案|是否正确");
						System.out.println("示例：5|正确或者不正确|6(分数))");
						String line = br.readLine();
						line = line.trim();
						String[] lines = line.split("\\|");
						if(lines.length != 3) 
						{
							System.out.println("你输入的格式有误");
							return;
						}
						Answer answer = new Answer();
						answer.setAnswer(lines[0]);
						answer.setFolg(lines[1]);
						answer.setCreateTime(new Date());
						answer.setEndTime(new Date());
						answer.setLevel(Double.valueOf(lines[2]));
						answer.setQuestionId(question.getId());
						answer.setSdId(students.getId());
						this.questionService.saveOneAnswerService(answer);
						if("正确".equalsIgnoreCase(answer.getFolg()))
						{
							/* 回答正确 */
							System.out.println("==此次回答over==");
							break ;
						}
					}
				}
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	private void help()
	{
		/*
		 * 1:查询所有朝代 2:添加朝代 3:修改朝代;(先输入朝代id,填写要修改的内容) 4:删除朝代 5:查询所有皇上 6:添加皇上
		 * 7:修改皇上;(先输入皇上id,填写要修改的内容) 8:删除皇上
		 */
		System.out.println("1:查询所有朝代");
		System.out.println("2:添加朝代");
		System.out.println("3:修改朝代;(先输入朝代id,填写要修改的内容)");
		System.out.println("4:删除朝代");
		System.out.println("5:查看单个朝代");
		System.out.println("6:查询所有皇上");
		System.out.println("7:添加皇上");
		System.out.println("8:修改皇上;(先输入皇上id,填写要修改的内容)");
		System.out.println("9:删除皇上");
		System.out.println("10:查询单个皇上");
		System.out.println("11:增加一个学生");
		System.out.println("12:查询所有学生");
		System.out.println("13:查询单个学生");
		System.out.println("14:修改学生;(先输入学生id,填写要修改的内容)");
		System.out.println("15:删除学生");
		System.out.println("16:增加一个学生等级");
		System.out.println("17:查询所有学生等级");
		System.out.println("18:查询一个学生等级");
		System.out.println("19:修改一个学生等级;(先输入段位id,填写要修改的内容)");
		System.out.println("20:删除一个学生等级");
		System.out.println("21:查询所有答案");
		System.out.println("22:添加答案");
		System.out.println("23:修改答案;(先输入答案id,填写要修改的内容)");
		System.out.println("24:删除答案");
		System.out.println("25:查看单个答案");
		System.out.println("26:查询所有问题");
		System.out.println("27:添加问题");
		System.out.println("28:修改问题;(先输入问题id,填写要修改的内容)");
		System.out.println("29:删除问题");
		System.out.println("30:查看单个问题");
	}

	private void deleteOneQuestion(BufferedReader br)
	{
		System.out.println("请输入要删除的id");
		Question question = this.selectOneQuestion(br);
		this.questionService.deleteOneQuestionService(question);
		System.out.println("删除成功");
	}

	private void updateOneQuestion(BufferedReader br)
	{
		Question question = this.selectOneQuestion(br);
		if (question == null)
		{
			return;
		}
		System.out.println(
				"更新后请按照示例输入，示例：" + question.getName() + "|" + question.getScore() + "|" + question.getPeopleNum());
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");
			if (lines.length != 3)
			{
				System.out.println("输入有误");
				return;
			}
			question.setName(lines[0]);
			question.setScore(Integer.valueOf(lines[1]));
			question.setPeopleNum(Integer.valueOf(lines[2]));
			question.setCreatTime(new Date());
			question.setEndTime(new Date());
			this.questionService.updateOneQuestionService(question);
			System.out.println("更新成功，id：" + question.getId());
		} catch (NumberFormatException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	private Question selectOneQuestion(BufferedReader br)
	{
		System.out.println("请输入要查询的问题的编号");
		Question question = new Question();
		try
		{
			String line = br.readLine();
			line = line.trim();
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", Integer.valueOf(line));
			question = this.questionService.selectOneQuestionService(condMap);
			if (question == null)
			{
				System.out.println("搜索不到此问题");
			} else
			{
				String title = this.questionService.printTitleAnswerService();
				System.out.println(title);
				System.out.println(question);
			}
			return question;
		} catch (NumberFormatException e)
		{
			// e.printStackTrace();
			System.out.println("输入的id应该为整数");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	private void saveOneQuestion(BufferedReader br)
	{
		System.out.println("请输入要保存的问题，示例：金|100|13");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");
			if (lines.length != 3)
			{
				System.out.println("输入有误");
				return;
			}
			Question question = new Question();
			question.setName(lines[0]);
			question.setScore(Integer.valueOf(lines[1]));
			question.setPeopleNum(Integer.valueOf(lines[2]));
			question.setCreatTime(new Date());
			question.setEndTime(new Date());
			this.questionService.saveOneQuestionService(question);
			System.out.println("保存成功，id：" + question.getId());
		} catch (NumberFormatException e)
		{
			// e.printStackTrace();
			System.out.println("输入有误");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	private void selectListQuestion()
	{
		List<Question> questionlist = new ArrayList<Question>();
		questionlist = this.questionService.selectListQuestionService();
		String title = this.questionService.printTitleQuestionService();
		System.out.println(title);
		for (Question question : questionlist)
		{
			System.out.println(question);
		}
	}

	private void deleteOneAnswer(BufferedReader br)
	{
		System.out.println("请输入要删除的id");
		Answer an = this.selectOneAnswer(br);
		if (an == null)
		{
			System.out.println("你要删除的内容不存在");
		} else
		{
			this.questionService.deleteOneAnswerService(an);
			System.out.println("删除成功");
		}

	}

	private void updateOneAnswer(BufferedReader br)
	{

		Answer an = this.selectOneAnswer(br);
		if (an == null)
		{
			return;
		}
		System.out.println("更新后请按照示例输入，示例：" + an.getSdId() + "|" + an.getAnswer() + "|" + an.getFolg());
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");
			an.setSdId(Integer.valueOf(lines[0]));
			an.setAnswer(lines[1]);
			an.setFolg(lines[2]);
			an.setEndTime(new Date());
			this.questionService.updateOneAnswerService(an);
			System.out.println("更新成功，id：" + an.getId());
		} catch (NumberFormatException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	private Answer selectOneAnswer(BufferedReader br)
	{
		System.out.println("=查询单个答案，请输入id=");
		Answer an = new Answer();
		try
		{
			String line = br.readLine();
			line = line.trim();
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", Integer.valueOf(line));
			an = this.questionService.selectOneAnswerService(condMap);
			if (an == null)
			{
				System.out.println("搜索不到此答案");
			} else
			{
				String title = this.questionService.printTitleAnswerService();
				System.out.println(title);
				System.out.println(an);
			}
			return an;
		} catch (NumberFormatException e)
		{
			// e.printStackTrace();
			System.out.println("输入的id应该为整数");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	private void saveOneAnswer(BufferedReader br)
	{
		System.out.println("请输入要保存的答案，示例：3|字节流|正确");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");
			if (lines.length != 3)
			{
				System.out.println("输入有误");
				return;
			}
			Answer an = new Answer();
			an.setSdId(Integer.valueOf(lines[0]));
			an.setAnswer(lines[1]);
			an.setFolg(lines[2]);
			an.setCreateTime(new Date());
			an.setEndTime(new Date());
			this.questionService.saveOneAnswerService(an);
			System.out.println("保存成功，id：" + an.getId());
		} catch (NumberFormatException e)
		{
			// e.printStackTrace();
			System.out.println("你输入的编号不正确");
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void selectListAnswer()
	{
		List<Answer> an = this.questionService.selectListAnswerService();
		String title = this.questionService.printTitleAnswerService();
		System.out.println(title);
		for (Answer answer : an)
		{
			System.out.println(answer.getId() + "\t" + answer.getSdId() + "\t" + answer.getAnswer() + "\t"
					+ answer.getFolg() + "\t\t\t" + dateutile.dateTimeToStr(answer.getCreateTime()) + "\t"
					+ dateutile.dateTimeToStr(answer.getEndTime()));
		}
	}

	/*
	 * 增加一个学生等级
	 */
	private void savaGradeOneDao(BufferedReader br)
	{
		System.out.println("请输入要存入的学生信息：(段位,起始分数,结束分数)");
		System.out.println("示例:黄金|17.0|18.0");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");
			if (lines.length != 3)
			{
				System.out.println("您输入的格式不正确！");
				return;
			}
			Grade grade = new Grade();
			grade.setGrading(lines[0]);
			grade.setStNum(Double.valueOf(lines[1]));
			grade.setEdNum(Double.valueOf(lines[2]));
			grade.setCreatDate(new Date());
			grade.setEndDate(new Date());
			this.studentservice.savaGradeOneDao(grade);
			System.out.println("存入成功，ID为：" + grade.getId());
		} catch (NumberFormatException e)
		{
//			e.printStackTrace();
			System.out.println("你应该输入数字");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/*
	 * 删除一个学生等级
	 */
	private void delectGradeOneDao(BufferedReader br)
	{

		Grade student = this.selectGradeOneDao(br);
		if (student != null)
		{
			this.studentservice.delectGradeOneDao(student);
		} else
		{
			System.out.println("没有此信息，请核对后操作");
		}
	}

	/*
	 * 修改一个学生等级
	 */
	private void updateGradeOneDao(BufferedReader br)
	{
		Grade grade = this.selectGradeOneDao(br);
		if (grade != null)
		{
			grade.setName("李明");
			this.studentservice.updateGradeOneDao(grade);
			System.out.println("修改成功");
		} else
		{
			System.out.println("你要修改的信息不存在，请核对后再进行修改");
		}
	}

	/*
	 * 查询一个学生等级
	 */
	private Grade selectGradeOneDao(BufferedReader br)
	{
		try
		{
			System.out.println("输入您要查询的ID号");
			String line = br.readLine();
			line = line.trim();
			Map<String, Object> OneMap = new HashMap<String, Object>();
			OneMap.put("id", Integer.valueOf(line));
			Grade student = this.studentservice.selectGradeOneDao(OneMap);
			System.out.println(student);
			return student;
		} catch (NumberFormatException e)
		{
//			e.printStackTrace();
			System.out.println("你输入的应该是整数");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * 查询所有学生等级
	 */
	private void selectGradeListDao()
	{
		List<Grade> list = this.studentservice.selectGradeListDao();
		String title = this.studentservice.printTitleGradeService();
		System.out.println(title);
		for (Grade students : list)
		{
			System.out.println(students);
		}
	}

	/*
	 * 增加一个学生
	 */
	private void savaStudentsOneDao(BufferedReader br)
	{ // 请依次输入学生的相关信息:姓名,性别,生日,毕业院校,分数,答对次数,答错次数
		// 示例:张三|男|1996-12-03|北大|30|3|4
		System.out.println("请输入要存入的学生信息：(名字|等级|性别|生日|大学)");
		System.out.println("示例:小明|3|男|1996-12-11|河南科技大学");

		try
		{
			String line = br.readLine();
			String[] lines = line.split("\\|");
			if (lines.length != 5)
			{
				System.out.println("您输入的格式不正确！");
				return;
			}
			Boolean sex;
			if ("男".equalsIgnoreCase(lines[2]))
			{
				sex = true;
			} else if ("女".equalsIgnoreCase(lines[2]))
			{
				sex = false;
			} else
			{
				System.out.println("性别输入错误");
				return;
			}
			Date birth = dateutile.strToDate(lines[3]);
			Students student = new Students();
			student.setName(lines[0]);
			student.setBirth(birth);
			student.setLevel(Double.valueOf(lines[1]));
			student.setGraduateSchool(lines[4]);
			student.setSex(sex);
			student.setCreateTime(new Date());
			student.setUpdateTime(new Date());
			this.studentservice.savaStudentsOneDao(student);
			System.out.println("添加成功,ID为" + student.getId());
		} catch (NumberFormatException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	/*
	 * 查询所有学生
	 */
	private void selectStudentsListDao()
	{
		List<Students> list = this.studentservice.selectStudentsListDao();
		String title = this.studentservice.printTitleStudentService();
		System.out.println(title);
		for (Students students : list)
		{
			System.out.println(students);
		}
	}

	/*
	 * 查询一个学生
	 */
	private Students selectStudentsOneDao(BufferedReader br)
	{
		System.out.println("请输入要查询的ID");
		try
		{
			String line = br.readLine();
			Map<String, Object> OneMap = new HashMap<String, Object>();
			OneMap.put("id", Integer.valueOf(line));
			Students student = this.studentservice.selectStudentsOneDao(OneMap);
			System.out.println(student);
			return student;
		} catch (NumberFormatException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * 修改一个学生
	 */
	private void updateStudentsOneDao(BufferedReader br)
	{
		System.out.println("请输入要修改学生的ID");
		Students student = this.selectStudentsOneDao(br);
		if (student == null)
		{
			System.out.println("此信息不存在，请核对后再修改");
			return;
		}
		
		try
		{
			System.out.println("示例:" + student.getName() + "|" + student.getLevel() + "|" + student.isSex());
			String line = br.readLine();
			String[] lines = line.split("\\|");
			if (lines.length != 3)
			{
				System.out.println("您输入的格式不正确！");
				return;
			}
			student.setName(lines[0]);
			student.setBirth(new Date());
			student.setLevel(Double.valueOf(lines[1]));
			student.setSex(Boolean.valueOf(lines[2]));
			student.setCreateTime(new Date());
			this.studentservice.updateStudentsOneDao(student);
			System.out.println("修改成功");
		} catch (NumberFormatException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/*
	 * 删除一个学生
	 */
	private void delectStudentsOneDao(BufferedReader br)
	{

		Students student = this.selectStudentsOneDao(br);
		if (student != null)
		{
			this.studentservice.delectStudentsOneDao(student);
		} else
		{
			System.out.println("没有此信息，请核对后操作");
		}

	}

	/*
	 * * 增加了一个朝代
	 */
	private void saveOneService(BufferedReader br)
	{
		System.out.println("请输入要存入的朝代信息：(名字|年龄|首都|开国时间)");
		System.out.println("示例:明朝|279|北京|1368-01-01");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");
			if (lines.length != 4)
			{
				System.out.println("时间格式有问题,会变成当前时间");
				return;
			}
			Date date = dateutile.strToDate(lines[3]);
			Dynasty dynasty = new Dynasty();
			dynasty.setName(lines[0]);
			dynasty.setAge(Short.valueOf("267"));
			dynasty.setCapital(lines[2]);
			dynasty.setSt(date);
			dynasty.setCreateTime(new Date());
			dynasty.setUpdateTime(new Date());
			int id = this.demoService.savaOneDynastyService(dynasty);
			System.out.println("保存成功" + id);
		} catch (NumberFormatException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	/*
	 * 查询所有的朝代
	 */
	private void selectListDynastyService()
	{
		List<Dynasty> list = this.demoService.selectListDynastyService();
		String title = this.demoService.printTitleDynastyService();
		System.out.println(title);
		for (Iterator iterator = list.iterator(); iterator.hasNext();)
		{
			Dynasty dynasty = (Dynasty) iterator.next();
			System.out.println(dynasty.getId() + "\t" + dynasty.getName() + "\t" + dynasty.getCapital() + "\t"
					+ dynasty.getAge() + "\t" + dateutile.dateTimeToStr(dynasty.getSt()) + "\t"
					+ dateutile.dateTimeToStr(dynasty.getCreateTime()) + "\t"
					+ dateutile.dateTimeToStr(dynasty.getUpdateTime()));
		}
	}

	/*
	 * 查询单个朝代
	 */
	private Dynasty selectOneDynastyService(BufferedReader br)
	{
		System.out.println("--查询单个朝代,请输入id--");
		try
		{
			String line = br.readLine();
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", Integer.valueOf(line));
			Dynasty dynasty = this.demoService.selectOneDynastyService(condMap);
			String title = this.demoService.printTitleDynastyService();
			System.out.println(title);
			System.out.println(dynasty);
			return dynasty;
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (NumberFormatException e)
		{
			System.out.println("您输入的应该是整数。");
		}
		return null;
	}

	/*
	 * 修改一条记录
	 */
	private void updateOneDynastyService(BufferedReader br)
	{
		Dynasty dynasty = this.selectOneDynastyService(br);
		if (dynasty == null)
		{
			System.out.println("没有此信息，请核对后修改！");
			return;
		}
		String templateStr = "更新的示例:" + dynasty.getName() + "|" + dynasty.getAge() + "|" + dynasty.getCapital() + "|"
				+ this.dateutile.dateToStr(dynasty.getSt());
		System.out.println(templateStr);
		try
		{
			String line = br.readLine();
			String[] lines = line.split("\\|");
			if (lines.length != 4)
			{
				System.out.println("输入的格式不正确");
				return;
			}
			Date stDate = dateutile.strToDate(lines[3]);
			dynasty.setName(lines[0]);
			dynasty.setAge(Short.valueOf(lines[1]));
			dynasty.setCapital(lines[2]);
			/* 如何把Calendar变成Date */
			dynasty.setSt(stDate);
			/* 记得更新的时候,不要给创建时间赋值 */
			dynasty.setUpdateTime(new Date());
			this.demoService.updateOneDynastyService(dynasty);

			System.out.println("修改成功，修改的ID为：" + dynasty.getId());
		} catch (NumberFormatException e)
		{
			System.out.println("==您输入的字符串变不成数字==");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/*
	 * 删除一条记录
	 */
	private void deleteOneDynastyService(BufferedReader br)
	{
		/* 根据id查询 */
		Dynasty dynasty = this.selectOneDynastyService(br);
		if (dynasty != null)
		{
			/* 调用删除方法 */
			this.demoService.deleteOneDynastyService(dynasty);
			System.out.println("==删除成功==id:" + dynasty.getId());
		} else
		{
			System.out.println("==此条记录不存在==");
		}
	}

	/*
	 * 查看所有皇上
	 */
	private void selectListKingService()
	{
		List<King> list = this.demoService.selectListKingService();
		String title = this.demoService.printTitleKingService();
		System.out.println(title);
		for (King king : list)
		{
			String dyname;
			Dynasty dynasty = king.getdynasty();
			if (dynasty != null)
			{
				dyname = dynasty.getName();
			} else
			{
				dyname = "无";
			}
			System.out.println(king.getId() + "\t" + dyname + "\t" + king.getName() + "\t" + king.getAge() + "\t"
					+ king.getHight() + "\t" + king.getMiaoHao() + "\t" + king.getNianHao() + "\t"
					+ dateutile.dateTimeToStr(king.getCreateTime()) + "\t"
					+ dateutile.dateTimeToStr(king.getUpdateTime()));
		}
	}

	/*
	 * 保存一个皇上
	 */
	private void savaOneKingService(BufferedReader br)
	{
		System.out.println("请输入要存入的皇上信息：(3|朱元璋|80|170.0|太祖|洪武)");
		System.out.println("示例:3|朱元璋|80|170.0|太祖|洪武");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");
			if (lines.length != 6)
			{
				System.out.println("您输入的格式不正确");
				return;
			}
			System.out.println(Arrays.toString(lines));
			King kingNew = new King();
			kingNew.setName(lines[1]);
			kingNew.setAge(Short.valueOf(lines[2]));
			kingNew.setDynastyId(Integer.valueOf(lines[0]));
			kingNew.setHight(Double.valueOf(lines[3]));
			kingNew.setMiaoHao(lines[4]);
			kingNew.setNianHao(lines[5]);
			kingNew.setCreateTime(new Date());
			kingNew.setUpdateTime(new Date());
			this.demoService.savaOneKingService(kingNew);
			int id = kingNew.getId();
			System.out.println("存入成功，ID为：" + id);
		} catch (NumberFormatException e)
		{
			System.out.println("您应该输入整数");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/*
	 * 修改一个皇上
	 */
	private void updateOneKingService(BufferedReader br)
	{
		King king = this.selectOneKingService(br);
		if (king != null)
		{
			king.setName("秦始皇");
			this.demoService.updateOneKingService(king);
			System.out.println("===修改成功===");
		} else
		{
			System.out.println("没有此条信息，请核对后修改！");
		}

	}

	/*
	 * 删除一个皇上
	 */
	private void deleteOneKingService(BufferedReader br)
	{
		King king = this.selectOneKingService(br);
		if (king != null)
		{
			this.demoService.deleteOneKingService(king);
			System.out.println("删除成功");
		} else
		{
			System.out.println("没有此信息，请核查清楚！");
		}
	}

	/*
	 * 查看一个皇上
	 */

	private King selectOneKingService(BufferedReader br)
	{
		System.out.println("请输入查询ID");
		try
		{
			String line = br.readLine();
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", Integer.valueOf(line));
			King king = this.demoService.selectOneKingService(condMap);
			System.out.println(king);
			return king;
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (NumberFormatException e)
		{
			System.out.println("您输入的应该是整数。");
		}
		return null;
	}

	public void setDemoService(IDemoService demoService)
	{
		this.demoService = demoService;
	}

	public void setQuestionService(IQuestionService questionService)
	{
		this.questionService = questionService;
	}

	public void setStudentservice(Istudentsservice studentservice)
	{
		this.studentservice = studentservice;
	}

}
